﻿using CefSharp;
using EDesk.kit;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EDesk.cefwrapper
{
    internal class KeyBoardHander : IKeyboardHandler
    {

        //标记是否全屏
        private Boolean isFullScreen = false;

        public bool OnKeyEvent(IWebBrowser chromiumWebBrowser, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey)
        {
            if(type == KeyType.KeyUp && Enum.IsDefined(typeof(Keys), windowsKeyCode))
            {
                var key = (Keys)windowsKeyCode;
                switch (key)
                {
                    case Keys.F11: // 满屏/恢复
                        isFullScreen = !isFullScreen;
                        MainForm mainForm = MainForm.ins;
                        mainForm.SuspendLayout(); //挂起控件布局变更
                        mainForm.FormBorderStyle = isFullScreen ? FormBorderStyle.None : FormBorderStyle.Sizable;
                        mainForm.TopMost = isFullScreen;
                        mainForm.WindowState = FormWindowState.Normal;
                        mainForm.WindowState = FormWindowState.Maximized;
                        mainForm.Activate();
                        mainForm.ResumeLayout(); //启动控件布局变更
                        break;
                    case Keys.F12: // 打开调试控制台
                        browser.ShowDevTools();
                        break;
                    case Keys.F5:
                        if (modifiers == CefEventFlags.ControlDown)
                            browser.Reload(true); // 强制刷新，忽略缓存
                        else
                            browser.Reload(); // 普通刷新
                        break;
                }
            }
            return false;
        }

        public bool OnPreKeyEvent(IWebBrowser chromiumWebBrowser, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut)
        {
            return false;
        }

    }
}
